﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.BUS;
using Restaurant.DTO;

namespace Restaurant
{
    public partial class frmQLNhanVien : Form
    {
        NhanVienService nvService = new NhanVienService();
        NhaHangService nhService = new NhaHangService();
        
        public frmQLNhanVien()
        {
            InitializeComponent();
        }

        private void LoadLoaiNV()
        {
            DataTable dbtab = nvService.LayTatCaLoaiNV();
            cbLoaiNV.Text = "Tất Cả";
            cbLoaiNV.Items.Add("Tất Cả");
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbdow = dbtab.Rows[i];
                cbLoaiNV.Items.Add(dbdow[1].ToString());
            }
            //cbLoaiNV.Text = "Tất Cả";
        }

        private void LoadNV(DataTable dbtab)
        {
            lv_NV.Items.Clear();
            //DataTable dbtab = nvService.LayTatCaNV();
            for (int i = 0; i < dbtab.Rows.Count;i++ )
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow[0].ToString();
                liv.SubItems.Add(dbrow[1].ToString());
                liv.SubItems.Add(dbrow[2].ToString());
                liv.SubItems.Add(dbrow[3].ToString());
                liv.SubItems.Add(dbrow[4].ToString());
                liv.SubItems.Add(dbrow[5].ToString());
                liv.SubItems.Add(nvService.LayLoaiNV(int.Parse(dbrow[6].ToString())));
                liv.SubItems.Add(nhService.LayTenNH(int.Parse(dbrow["MaNH"].ToString())));
                liv.SubItems.Add(dbrow[9].ToString());
                lv_NV.Items.Add(liv);
            }
        }

        private void frmQLNhanVien_Load(object sender, EventArgs e)
        {
            LoadLoaiNV();
            DataTable dbtab = nvService.LayTatCaNV();
            LoadNV(dbtab);
        }

        private void cbLoaiNV_SelectedIndexChanged(object sender, EventArgs e)
        {
            //string tenloai = cbLoaiNV.SelectedItem.ToString();
            int index = cbLoaiNV.SelectedIndex;
            string tenloai = cbLoaiNV.Items[index].ToString();
            if (tenloai == "Tất Cả")
            {
                DataTable dbtab = nvService.LayTatCaNV();
                LoadNV(dbtab);
                return;
            }
            int maloai = nvService.LayMaLoaiNV(tenloai);
            LoadNVTheoLoai(maloai);
        }

        private void LoadNVTheoLoai(int maloai)
        {
            DataTable dbtab = nvService.LayNVTheoLoai(maloai);
            LoadNV(dbtab);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string ten = textBox1.Text;
            DataTable dbtab = nvService.TimNVTheoTen(ten);
            LoadNV(dbtab);
        }

        private void btnThemNhanVien_Click(object sender, EventArgs e)
        {

        }

        private void btnXoaNhanVien_Click(object sender, EventArgs e)
        {

        }
    }
}
